<template>
  <div class="wangeditor-container">
    <el-form ref="editorFormRef" :model="dynamicValidateForm" label-width="50px" class="editor-form">
      <el-form-item
        prop="title"
        label="标题"
        :rules="[{ required: true, message: '请输入标题', trigger: 'blur' }]"
      >
        <el-input
          v-model="dynamicValidateForm.title"
          class="common-input editor-input"
        />
      </el-form-item>
      <el-form-item
        prop="content"
        label="内容"
        :rules="[{ required: true, message: '请输入内容', trigger: 'blur' }]"
      >
        <WangEditor :content="dynamicValidateForm.content" @setContent="setContent"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="common-button" @click="submitForm('editorFormRef')">保存</el-button>
        <el-button type="primary" class="common-button" @click="preview('editorFormRef')">预览</el-button>
        <el-button class="common-button" @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="text-word">
      <div class="text-name">参考文档</div>
      <div class="text-link">
        <a href="https://www.wangeditor.com/v5/for-frame.html" target="_blank">
          https://www.wangeditor.com/v5/for-frame.html </a>
      </div>
    </div>
    <div class="preview-dialog-container">
      <!--   close-on-click-modal: 点击弹窗以外的空白处是否可以关闭弹窗，默认值为true（可以关闭）   -->
      <!--   close-on-press-escape: 是否可以通过按下 ESC 关闭 Dialog，默认值为true（可以关闭）   -->
      <el-dialog
        class="preview-dialog"
        title="预览"
        :visible="dialogVisible"
        :before-close="closeAct"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
      >
        <div class="context-title">
          <div class="t-title">标题：</div>
          <div class="title-content">{{ dynamicValidateForm.title }}</div>
        </div>
        <div class="context-content">
          <div class="c-title">内容：</div>
          <div class="text-content" v-html="dynamicValidateForm.content"></div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import WangEditor from '@/components/WangEditor/index.vue'
export default {
  name: 'index',
  components:{
    WangEditor,
  },
  data(){
    return {
      dynamicValidateForm: {
        title: '',
        content: '',
      },
      dialogVisible: false,
    }
  },
  methods:{
    submitForm(formRef){
      this.$refs[formRef].validate((valid) => {
        if(valid){
          this.$message.success('保存成功')
        }else{
          console.log('error submit!')
          return false
        }
      })
    },
    preview(formRef){
      this.$refs[formRef].validate((valid) => {
        if(valid){
          this.dialogVisible = true
        }else{
          console.log('error submit!')
          return false
        }
      })
    },
    // 重置表单
    resetForm(){
      this.dynamicValidateForm.title = ''
      this.dynamicValidateForm.content = ''
    },
    setContent(val){
      this.dynamicValidateForm.content = val
    },
    closeAct(){
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.wangeditor-container{
  background: white;
  height: calc(100vh - 190px);
  box-shadow: 0 0 12px rgba(0, 0,0 , 0.05);
  padding: 20px;
  text-align: left;
  border-radius: 4px;
}
.text-word{
  display: flex;
  line-height: 32px;
  padding-left: 50px;
}
.text-link{
  margin-left: 10px;
}
.context-title{
  display: flex;
  font-size: 20px;
  font-weight: 600;
}
.common-text{
  margin-left: 10px;
}
.context-content{
  display: flex;
  margin-top: 20px;
}
.text-content{
  flex: 1;
}
.c-title{
  font-weight: 600;
}
</style>

<style lang="scss">
.preview-dialog{
  .el-dialog{
    width: 100%;
    height: 100vh;
    margin: 0 auto !important;
  }
  .el-dialog__header{
    text-align: left;
    font-weight: 600;
    background: $dialogHeaderBgColor;
  }
  .el-dialog__title{
    color: $fontColor;
  }
}
// 预览表格样式
.text-content{
  table{
    border: 1px solid #d8d8d8;
    border-collapse: collapse;
    border-spacing: 2px;
    width: 100%;
    th{
      background-color: #f5f2f0;
      border: 1px solid #d8d8d8;
      padding: 3px 5px;
      text-align: center;
      line-height: 1.5;
    }
    td{
      border: 1px solid #d8d8d8;
      padding: 3px 5px;
      text-align: left;
      line-height: 1.5;
    }
  }
}
</style>
